<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>新增角色</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/static/admin/lib/layui-v2.6.3/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
  <div class="layuimini-main">

    <form class="layui-form" action="">
      <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
          <input type="text" name="role_name"  autocomplete="off" placeholder="请输入角色名称" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">所属权限</label>
        <div class="layui-input-block" id="tree">
        </div>
      </div>

      <div class="layui-form-item">
        <div class="layui-input-block">
          <button type="button" class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>

  </div>
</div>
<script src="/static/admin/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
  layui.use(['form',"tree"], function () {
    var form = layui.form
            , layer = layui.layer
            , tree = layui.tree
            ,$ = layui.jquery;

    // 获取权限
    getMenuList();

    function getMenuList(){
      $.ajax({
        url : '/back/role/getMenuLists',
        type : 'GET',
        success : function(res){
          if(res.code == 1){
            var inst = tree.render({
              elem : '#tree',
              data : res.data,
              id : 'id',
              showCheckbox : true,
            });
          }
        }
      });
    }

    //监听提交
    form.on('submit(sub)', function (data) {
      data = data.field;
      data.powers = tree.getChecked('id');
      if (data.role_name == '') {
        layer.msg('角色名称不能为空');
        return false;
      }
      if (data.powers == '') {
        layer.msg('所属权限不能为空');
        return false;
      }
      $.ajax({
        url : '/back/role/add',
        data : data,
        type : 'POST',
        success : function (res){
          if(res.code == 1){
            layer.msg(res.msg,'',function(){
              var index = parent.layer.getFrameIndex(window.name);
              parent.location.reload();
              parent.layer.close(index);
            });
          }else{
            layer.msg(res.msg);
          }
        }
      });
    });



  });
</script>

</body>
</html>